home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex7.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  3KB  |  70 lines

  1. #include <genstub.c>
  2.  
  3. CRITICAL_SECTION cs;
  4.  
  5. // Child thread procedure.
  6. DWORD WINAPI ChildThreadProc(HWND hWnd)
  7. {
  8.    TCHAR szBuffer[256];    // Work area for print formatting.
  9.    wsprintf( szBuffer, "Thread %x waiting for critical section", GetCurrentThreadId() );
  10.    SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  11.    // Enter protected code.
  12.    EnterCriticalSection( &cs );
  13.    wsprintf( szBuffer,"Thread %x in critical section", GetCurrentThreadId() );
  14.    SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  15.    Sleep( 5000 );  // This is where our critical code would go if we had some.
  16.    // All done now, let someone else in.
  17.    LeaveCriticalSection( &cs );
  18.    wsprintf( szBuffer,"Thread %x has exited critical section", GetCurrentThreadId() );
  19.    SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  20.    ExitThread(TRUE);
  21. }
  22.  
  23. // Main Window Procedure
  24. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  25. {
  26.    switch (uMsg)
  27.    {
  28.            case WM_CREATE:  // A critical section must be initialized before it is used.
  29.                    InitializeCriticalSection(&cs);
  30.                    return DefWindowProc( hWnd, uMsg, wParam, lParam );
  31.            case WM_COMMAND:       // Process menu items.
  32.                    switch ( LOWORD( wParam ) )
  33.                    {
  34.                         case IDM_TEST:
  35.                         {
  36.                            DWORD dwChildID;
  37.                            CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &dwChildID );
  38.                         }
  39.                         break;
  40.                         case IDM_EXIT:
  41.                            DestroyWindow( hWnd );
  42.                            break;
  43.                    }
  44.            break;
  45.            case WM_USER:
  46.                    {  // Show synchronization activity.
  47.                       TCHAR szBuffer[101];
  48.                       static int row = 0;
  49.                       static int msg_num = 1;
  50.                       HDC hDC = GetDC( hWnd );
  51.                       FillMemory( szBuffer, 100, 32 );
  52.                       TextOut( hDC, 0, row, szBuffer, 100 );
  53.                       wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  54.                       TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  55.                       if ( row > 200 )
  56.                          row = 0;
  57.                       else
  58.                          row += 20;
  59.                       ReleaseDC( hWnd, hDC );
  60.                    }
  61.                    break;
  62.            case WM_DESTROY: // Get back resources from critical section object.
  63.                    DeleteCriticalSection(&cs);
  64.                    PostQuitMessage( 0 );
  65.                    break;
  66.            default:
  67.                 return DefWindowProc( hWnd, uMsg, wParam, lParam );
  68.    }
  69.    return NULL;
  70. }